<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">
<script>
    (function () {
        if ('') {
            if (prompt('请输入文章密码') !== '') {
                alert('密码错误！');
                if (history.length === 1) {
                    location.replace("https://google.com"); // 这里替换成你的首页
                } else {
                    history.back();
                }
            }
        }
    })();
</script>








<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="公众号,">










<meta name="description" content="知识星球是什么?  知识星球是创作者连接铁杆粉丝，做出高品质社群，实现知识变现的工具。创作者可以用知识星球连接铁杆粉丝，做出高品质社群，实现知识变现。知识星球解决的核心问题是社群收费管理难问题和内容不能沉淀问题。微信公众号、微博和行业专家——这些有粉丝的创作者是知识星球的核心用户，都可以用知识星球运营社群，知识变现。  以上来自知识星球官网的介绍 https://help.zsxq.com/ 口号">
<meta name="keywords" content="公众号">
<meta property="og:type" content="article">
<meta property="og:title" content="Python 抓取知识星球内容生成 PDF">
<meta property="og:url" content="http://yoursite.com/2019/09/16/Python-抓取知识星球内容生成-PDF/index.html">
<meta property="og:site_name" content="苏生不惑的博客">
<meta property="og:description" content="知识星球是什么?  知识星球是创作者连接铁杆粉丝，做出高品质社群，实现知识变现的工具。创作者可以用知识星球连接铁杆粉丝，做出高品质社群，实现知识变现。知识星球解决的核心问题是社群收费管理难问题和内容不能沉淀问题。微信公众号、微博和行业专家——这些有粉丝的创作者是知识星球的核心用户，都可以用知识星球运营社群，知识变现。  以上来自知识星球官网的介绍 https://help.zsxq.com/ 口号">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-840f9df6851655c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-935a32b13ae6980f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-c78802c9c772446f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-0bc2f593ae8857eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-2a3fe75088709eb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-07b0a8ce1442ea8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-87020a336d8967cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-5f36737e35dbbff9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-aeca99f305f23d61.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-dc036a80532a5ce1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-70a44013887caa7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-1818e57b45d5027b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-1a497d8234026372.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-a86943499b797fd1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-aace6c380516b87f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-ec6b0234c3353915.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-b20148873f1dc5a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-2211005aef836d43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-5d2c9bc76a82c136.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-410eb89d1155c975.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-2b1906c35e8a37da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-5bd614a2d0559d20.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:image" content="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">
<meta property="og:updated_time" content="2019-10-11T09:26:57.939Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Python 抓取知识星球内容生成 PDF">
<meta name="twitter:description" content="知识星球是什么?  知识星球是创作者连接铁杆粉丝，做出高品质社群，实现知识变现的工具。创作者可以用知识星球连接铁杆粉丝，做出高品质社群，实现知识变现。知识星球解决的核心问题是社群收费管理难问题和内容不能沉淀问题。微信公众号、微博和行业专家——这些有粉丝的创作者是知识星球的核心用户，都可以用知识星球运营社群，知识变现。  以上来自知识星球官网的介绍 https://help.zsxq.com/ 口号">
<meta name="twitter:image" content="https://upload-images.jianshu.io/upload_images/17817191-840f9df6851655c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/09/16/Python-抓取知识星球内容生成-PDF/">



<meta name="referrer" content="never"> ​​​​


  <title>Python 抓取知识星球内容生成 PDF | 苏生不惑的博客</title>
  








</head>

<body itemscope="" itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope="" itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">苏生不惑的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br>
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off" placeholder="搜索..." spellcheck="false" type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope="" itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/09/16/Python-抓取知识星球内容生成-PDF/">

    <span hidden itemprop="author" itemscope="" itemtype="http://schema.org/Person">
      <meta itemprop="name" content="苏生不惑">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope="" itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="苏生不惑的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Python 抓取知识星球内容生成 PDF</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-09-16T20:29:10+08:00">
                2019-09-16
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv"></span>次
            </span>
          

          
            <div class="post-wordcount">
              
                
                  <span class="post-meta-divider">|</span>
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4.4k 字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  21 分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>知识星球是什么?</p>
<blockquote>
<p>知识星球是创作者连接铁杆粉丝，做出高品质社群，实现知识变现的工具。创作者可以用知识星球连接铁杆粉丝，做出高品质社群，实现知识变现。<br>知识星球解决的核心问题是社群收费管理难问题和内容不能沉淀问题。微信公众号、微博和行业专家——这些有粉丝的创作者是知识星球的核心用户，都可以用知识星球运营社群，知识变现。</p>
</blockquote>
<p>以上来自知识星球官网的介绍 <a href="https://help.zsxq.com/" target="_blank" rel="noopener">https://help.zsxq.com/</a> 口号是连接1000位铁杆粉丝。</p>
<h3 id="为什么用星球"><a href="#为什么用星球" class="headerlink" title="为什么用星球"></a>为什么用星球</h3><p>我没做过社群，也不是什么行业专家，毕竟不是什么大v，为什么要用知识星球呢？主要是现在获取的资讯太多了，想沉淀记录些东西，方便自己，也方便他人找，为什么不用微博呢？微博用了很多年，每天都在更新，目前已经8万多条微博了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-840f9df6851655c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>微博上有很多mark党，不断转发微博，但几乎没再去看过，不知道你是否也这样，以为收藏就看过了，其实只是种心里安慰。</p>
<p>而且微博上转发的东西经常被删，微博太多管理起来也麻烦，于是6月份的时候建立了一个免费的星球，就是这个了<a href="https://wx.zsxq.com/dweb2/index/group/141281112142" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/141281112142</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-935a32b13ae6980f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-393b26173c148690.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="免费星球"><br>星球还可以上传文件，和微博一样加标签方便分类，还提供网页版，比微信方便多了。</p>
<h3 id="导出星球"><a href="#导出星球" class="headerlink" title="导出星球"></a>导出星球</h3><p>过去3个月更新几百条信息了，也都加好标签。现在有200多个小伙伴了，你有兴趣也加入吧。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-c78802c9c772446f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-0bc2f593ae8857eb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>比如工具这个标签列表的内容<a href="https://wx.zsxq.com/dweb2/index/tags/%E5%B7%A5%E5%85%B7/828821151512" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/tags/%E5%B7%A5%E5%85%B7/828821151512</a><br><img src="https://upload-images.jianshu.io/upload_images/17817191-2a3fe75088709eb8.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>但内容多了以后翻起来也麻烦，于是想着下载下来看，最好能导出PDF，于是准备研究下，搜索下发现有人已经做过了 <a href="https://github.com/wbsabc/zsxq-spider" target="_blank" rel="noopener">https://github.com/wbsabc/zsxq-spider</a> <a href="https://github.com/96chh/crawl-zsxq" target="_blank" rel="noopener">https://github.com/96chh/crawl-zsxq</a><br>思路为抓取网页版的接口<a href="https://api.zsxq.com/v1.10/groups/141281112142/topics?scope=all&amp;count=20" target="_blank" rel="noopener">https://api.zsxq.com/v1.10/groups/141281112142/topics?scope=all&amp;count=20</a> 每次加载20条，每次的最后一条的create_time为下次的开始时间，如果没有20条说明加载完了。不过他的代码还有些问题，需要改动下，于是开始动手了。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br><span class="line">198</span><br><span class="line">199</span><br><span class="line">200</span><br><span class="line">201</span><br><span class="line">202</span><br><span class="line">203</span><br><span class="line">204</span><br><span class="line">205</span><br><span class="line">206</span><br><span class="line">207</span><br><span class="line">208</span><br><span class="line">209</span><br><span class="line">210</span><br><span class="line">211</span><br><span class="line">212</span><br><span class="line">213</span><br><span class="line">214</span><br><span class="line">215</span><br><span class="line">216</span><br><span class="line">217</span><br><span class="line">218</span><br><span class="line">219</span><br><span class="line">220</span><br><span class="line">221</span><br><span class="line">222</span><br><span class="line">223</span><br><span class="line">224</span><br><span class="line">225</span><br><span class="line">226</span><br><span class="line">227</span><br><span class="line">228</span><br><span class="line">229</span><br><span class="line">230</span><br><span class="line">231</span><br><span class="line">232</span><br><span class="line">233</span><br><span class="line">234</span><br><span class="line">235</span><br><span class="line">236</span><br><span class="line">237</span><br><span class="line">238</span><br><span class="line">239</span><br><span class="line">240</span><br><span class="line">241</span><br><span class="line">242</span><br><span class="line">243</span><br><span class="line">244</span><br><span class="line">245</span><br><span class="line">246</span><br><span class="line">247</span><br><span class="line">248</span><br><span class="line">249</span><br><span class="line">250</span><br><span class="line">251</span><br><span class="line">252</span><br><span class="line">253</span><br><span class="line">254</span><br><span class="line">255</span><br><span class="line">256</span><br><span class="line">257</span><br><span class="line">258</span><br><span class="line">259</span><br><span class="line">260</span><br><span class="line">261</span><br><span class="line">262</span><br><span class="line">263</span><br><span class="line">264</span><br><span class="line">265</span><br><span class="line">266</span><br><span class="line">267</span><br><span class="line">268</span><br><span class="line">269</span><br><span class="line">270</span><br><span class="line">271</span><br><span class="line">272</span><br><span class="line">273</span><br><span class="line">274</span><br><span class="line">275</span><br><span class="line">276</span><br><span class="line">277</span><br><span class="line">278</span><br><span class="line">279</span><br><span class="line">280</span><br><span class="line">281</span><br><span class="line">282</span><br><span class="line">283</span><br><span class="line">284</span><br><span class="line">285</span><br><span class="line">286</span><br><span class="line">287</span><br><span class="line">288</span><br><span class="line">289</span><br><span class="line">290</span><br><span class="line">291</span><br><span class="line">292</span><br><span class="line">293</span><br><span class="line">294</span><br><span class="line">295</span><br><span class="line">296</span><br><span class="line">297</span><br><span class="line">298</span><br><span class="line">299</span><br><span class="line">300</span><br><span class="line">301</span><br><span class="line">302</span><br><span class="line">303</span><br><span class="line">304</span><br><span class="line">305</span><br><span class="line">306</span><br><span class="line">307</span><br><span class="line">308</span><br><span class="line">309</span><br><span class="line">310</span><br><span class="line">311</span><br><span class="line">312</span><br><span class="line">313</span><br><span class="line">314</span><br><span class="line">315</span><br><span class="line">316</span><br><span class="line">317</span><br><span class="line">318</span><br><span class="line">319</span><br><span class="line">320</span><br><span class="line">321</span><br><span class="line">322</span><br><span class="line">323</span><br><span class="line">324</span><br><span class="line">325</span><br><span class="line">326</span><br><span class="line">327</span><br><span class="line">328</span><br><span class="line">329</span><br><span class="line">330</span><br><span class="line">331</span><br><span class="line">332</span><br><span class="line">333</span><br><span class="line">334</span><br><span class="line">335</span><br><span class="line">336</span><br><span class="line">337</span><br><span class="line">338</span><br><span class="line">339</span><br><span class="line">340</span><br><span class="line">341</span><br><span class="line">342</span><br><span class="line">343</span><br><span class="line">344</span><br><span class="line">345</span><br><span class="line">346</span><br><span class="line">347</span><br><span class="line">348</span><br><span class="line">349</span><br><span class="line">350</span><br><span class="line">351</span><br><span class="line">352</span><br><span class="line">353</span><br><span class="line">354</span><br><span class="line">355</span><br><span class="line">356</span><br><span class="line">357</span><br><span class="line">358</span><br><span class="line">359</span><br><span class="line">360</span><br><span class="line">361</span><br><span class="line">362</span><br><span class="line">363</span><br><span class="line">364</span><br><span class="line">365</span><br><span class="line">366</span><br><span class="line">367</span><br><span class="line">368</span><br><span class="line">369</span><br><span class="line">370</span><br><span class="line">371</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> re</span><br><span class="line"><span class="keyword">import</span> requests</span><br><span class="line"><span class="keyword">import</span> json</span><br><span class="line"><span class="keyword">import</span> os</span><br><span class="line"><span class="keyword">import</span> pdfkit</span><br><span class="line"><span class="keyword">import</span> shutil</span><br><span class="line"><span class="keyword">import</span> datetime</span><br><span class="line"><span class="keyword">import</span> urllib.request</span><br><span class="line"><span class="keyword">from</span> bs4 <span class="keyword">import</span> BeautifulSoup</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> quote</span><br><span class="line"><span class="keyword">from</span> urllib.parse <span class="keyword">import</span> unquote</span><br><span class="line"><span class="keyword">import</span> re,jieba,pandas <span class="keyword">as</span> pd</span><br><span class="line"><span class="keyword">import</span> matplotlib.pyplot <span class="keyword">as</span> plt</span><br><span class="line"><span class="keyword">from</span> wordcloud <span class="keyword">import</span> WordCloud, STOPWORDS, ImageColorGenerator</span><br><span class="line"><span class="keyword">import</span> numpy <span class="keyword">as</span> np</span><br><span class="line"><span class="keyword">from</span> pyquery <span class="keyword">import</span> PyQuery</span><br><span class="line">ZSXQ_ACCESS_TOKEN = '2B9BA4A2-8CAB-3A7C-32E7-' # 登录后Cookie中的Token</span><br><span class="line">GROUP_ID = '142452821282'                                  # 知识星球中的小组ID 141281112142  每日分享554228114224</span><br><span class="line">PDF_FILE_NAME = '知识星球_斜杠星球.pdf'                               # 生成PDF文件的名字</span><br><span class="line">DOWLOAD_PICS = False                                        # 是否下载图片 True | False 下载会导致程序变慢</span><br><span class="line">DOWLOAD_COMMENTS = False                                    # 是否下载评论</span><br><span class="line">ONLY_DIGESTS = False                                       # True-只精华 | False-全部</span><br><span class="line">FROM_DATE_TO_DATE = False                                  # 按时间区间下载</span><br><span class="line">EARLY_DATE = '2017-05-25T00:00:00.000+0800'                # 最早时间 当FROM_DATE_TO_DATE=True时生效 为空表示不限制 形如'2017-05-25T00:00:00.000+0800'</span><br><span class="line">LATE_DATE = '2019-09-25T00:00:00.000+0800'                 # 最晚时间 当FROM_DATE_TO_DATE=True时生效 为空表示不限制 形如'2017-05-25T00:00:00.000+0800'</span><br><span class="line">DELETE_PICS_WHEN_DONE = True                               # 运行完毕后是否删除下载的图片</span><br><span class="line">DELETE_HTML_WHEN_DONE = True                               # 运行完毕后是否删除生成的HTML</span><br><span class="line">COUNTS_PER_TIME = 30                                       # 每次请求加载几个主题 最大可设置为30</span><br><span class="line">DEBUG = False                                              # DEBUG开关</span><br><span class="line">DEBUG_NUM = 120                                            # DEBUG时 跑多少条数据后停止 需与COUNTS_PER_TIME结合考虑</span><br><span class="line"></span><br><span class="line">html_template = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">&lt;!DOCTYPE html&gt;</span></span><br><span class="line"><span class="string">&lt;html lang="</span>en<span class="string">"&gt;</span></span><br><span class="line"><span class="string">&lt;head&gt;</span></span><br><span class="line"><span class="string">    &lt;meta charset="</span>UTF<span class="number">-8</span><span class="string">"&gt;</span></span><br><span class="line"><span class="string">&lt;/head&gt;</span></span><br><span class="line"><span class="string">&lt;body&gt;</span></span><br><span class="line"><span class="string">&lt;h1&gt;&#123;title&#125;&lt;/h1&gt;</span></span><br><span class="line"><span class="string">&lt;br&gt;&#123;author&#125; - &#123;cretime&#125;&lt;br&gt;</span></span><br><span class="line"><span class="string">&lt;p&gt;&#123;text&#125;&lt;/p&gt;</span></span><br><span class="line"><span class="string">&lt;/body&gt;</span></span><br><span class="line"><span class="string">&lt;/html&gt;</span></span><br><span class="line"><span class="string">"</span><span class="string">""</span></span><br><span class="line">htmls = []</span><br><span class="line">num = <span class="number">0</span></span><br><span class="line">def headers_to_dict(headers):</span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    将字符串</span></span><br><span class="line"><span class="string">    '''</span></span><br><span class="line"><span class="string">    Host: mp.weixin.qq.com</span></span><br><span class="line"><span class="string">    Connection: keep-alive</span></span><br><span class="line"><span class="string">    Cache-Control: max-age=</span></span><br><span class="line"><span class="string">    '''</span></span><br><span class="line"><span class="string">    转换成字典类型</span></span><br><span class="line"><span class="string">    :param headers: str</span></span><br><span class="line"><span class="string">    :return: dict</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    headers = headers.split(<span class="string">"\n"</span>)</span><br><span class="line">    d_headers = dict()</span><br><span class="line">    <span class="keyword">for</span> h <span class="keyword">in</span> headers:</span><br><span class="line">        h = h.strip()</span><br><span class="line">        <span class="keyword">if</span> h:</span><br><span class="line">            k, v = h.split(<span class="string">":"</span>, <span class="number">1</span>)</span><br><span class="line">            d_headers[k] = v.strip()</span><br><span class="line">    <span class="keyword">return</span> d_headers</span><br><span class="line">def get_data(url):</span><br><span class="line"></span><br><span class="line">    OVER_DATE_BREAK = False</span><br><span class="line"></span><br><span class="line">    global htmls, num</span><br><span class="line">        </span><br><span class="line">   </span><br><span class="line">    header = <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">Accept:application/json, text/plain, */*</span></span><br><span class="line"><span class="string">Accept-Encoding:gzip, deflate, br</span></span><br><span class="line"><span class="string">Accept-Language:zh-CN</span></span><br><span class="line"><span class="string">Connection:keep-alive</span></span><br><span class="line"><span class="string">Cookie:sensorsdata2015jssdkcross=%7B%22dis ; zsxq_access_token=BA53 6025A</span></span><br><span class="line"><span class="string">DNT:1</span></span><br><span class="line"><span class="string">Host:api.zsxq.com</span></span><br><span class="line"><span class="string">Origin:https://wx.zsxq.com</span></span><br><span class="line"><span class="string">Referer:https://wx.zsxq.com/dweb2/index/group/224445125221</span></span><br><span class="line"><span class="string">User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.79 Safari/537.36 Maxthon/5.2.7.5000</span></span><br><span class="line"><span class="string">X-Request-Id:fcf935489-5997-674e-9db2-a02ea9b389f</span></span><br><span class="line"><span class="string">X-Signature:81b69d88df26231776893554ec8d733448743908</span></span><br><span class="line"><span class="string">X-Timestamp:1567682270</span></span><br><span class="line"><span class="string">X-Version:1.10.17</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    headers = headers_to_dict(header)</span><br><span class="line">    t = []</span><br><span class="line">    </span><br><span class="line">    rsp = requests.get(url, headers=headers)</span><br><span class="line">    print(url,headers,rsp.json())</span><br><span class="line">    <span class="string">""</span><span class="string">"</span></span><br><span class="line"><span class="string">    cat temp.json</span></span><br><span class="line"><span class="string">    &#123;</span></span><br><span class="line"><span class="string">      "</span>succeeded<span class="string">": true,</span></span><br><span class="line"><span class="string">      "</span>resp_data<span class="string">": &#123;</span></span><br><span class="line"><span class="string">        "</span>topics<span class="string">": []</span></span><br><span class="line"><span class="string">      &#125;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    cat temp.css</span></span><br><span class="line"><span class="string">    h1 &#123;font-size:40px; color:red; text-align:center;&#125;</span></span><br><span class="line"><span class="string">    p &#123;font-size:30px;&#125;</span></span><br><span class="line"><span class="string">    img&#123;</span></span><br><span class="line"><span class="string">    	max-width:100%;</span></span><br><span class="line"><span class="string">    	margin:20px auto;</span></span><br><span class="line"><span class="string">    	height:auto;</span></span><br><span class="line"><span class="string">    	border:0;</span></span><br><span class="line"><span class="string">    	outline:0</span></span><br><span class="line"><span class="string">    	-webkit-box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        -moz-box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        box-shadow: 1px 4px 16px 8px #5CA2BE;</span></span><br><span class="line"><span class="string">        /*set the images aligned*/</span></span><br><span class="line"><span class="string">        display: block;</span></span><br><span class="line"><span class="string">        margin-left: auto;</span></span><br><span class="line"><span class="string">        margin-right: auto;</span></span><br><span class="line"><span class="string">    &#125;</span></span><br><span class="line"><span class="string">    "</span><span class="string">""</span></span><br><span class="line">    with open('temp.json', 'w', encoding='utf-8') as f: # 将返回数据写入temp.json方便查看</span><br><span class="line">        f.write(json.dumps(rsp.json(), indent=<span class="number">2</span>, ensure_ascii=False))</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">with</span> open(<span class="string">'temp.json'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f, open(<span class="string">'contents2.txt'</span>, <span class="string">'a+'</span>, encoding=<span class="string">'utf-8'</span>) <span class="keyword">as</span> f2:</span><br><span class="line">        <span class="keyword">for</span> topic <span class="keyword">in</span> json.loads(f.read()).get(<span class="string">'resp_data'</span>).get(<span class="string">'topics'</span>):</span><br><span class="line">            <span class="keyword">if</span> FROM_DATE_TO_DATE and EARLY_DATE.strip():</span><br><span class="line">                <span class="keyword">if</span> topic.get(<span class="string">'create_time'</span>) &lt; EARLY_DATE.strip():</span><br><span class="line">                    OVER_DATE_BREAK = True</span><br><span class="line">                    <span class="keyword">break</span></span><br><span class="line"></span><br><span class="line">            content = topic.get(<span class="string">'question'</span>, topic.get(<span class="string">'talk'</span>, topic.get(<span class="string">'task'</span>, topic.get(<span class="string">'solution'</span>))))</span><br><span class="line"></span><br><span class="line">            anonymous = content.get(<span class="string">'anonymous'</span>)</span><br><span class="line">            <span class="keyword">if</span> anonymous:</span><br><span class="line">                author = <span class="string">'匿名用户'</span></span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                author = content.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>)</span><br><span class="line"></span><br><span class="line">            cretime = (topic.get(<span class="string">'create_time'</span>)[:<span class="number">23</span>]).replace(<span class="string">'T'</span>, <span class="string">' '</span>)</span><br><span class="line"></span><br><span class="line">            text = content.get(<span class="string">'text'</span>, <span class="string">''</span>)</span><br><span class="line">            f2.write(text)</span><br><span class="line"></span><br><span class="line">            text = handle_link(text)</span><br><span class="line">            #f2.write(PyQuery(text).text())</span><br><span class="line">            t.append(text)</span><br><span class="line">            title = str(num) + <span class="string">'_'</span> + cretime[:<span class="number">16</span>]</span><br><span class="line">            num += <span class="number">1</span></span><br><span class="line">            <span class="keyword">if</span> topic.get(<span class="string">'digested'</span>) == True:</span><br><span class="line">                title += <span class="string">'_精华'</span></span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> DOWLOAD_PICS and content.get(<span class="string">'images'</span>):</span><br><span class="line">                soup = BeautifulSoup(html_template, <span class="string">'html.parser'</span>)</span><br><span class="line">                images_index = <span class="number">0</span></span><br><span class="line">                <span class="keyword">for</span> img <span class="keyword">in</span> content.get(<span class="string">'images'</span>):</span><br><span class="line">                    url = img.get(<span class="string">'large'</span>).get(<span class="string">'url'</span>)</span><br><span class="line">                    local_url = <span class="string">'./images/'</span> + str(num - <span class="number">1</span>) + <span class="string">'_'</span> + str(images_index) + <span class="string">'.jpg'</span></span><br><span class="line">                    images_index += <span class="number">1</span></span><br><span class="line">                    urllib.request.urlretrieve(url, local_url)</span><br><span class="line">                    img_tag = soup.new_tag(<span class="string">'img'</span>, src=local_url)</span><br><span class="line">                    soup.body.append(img_tag)</span><br><span class="line">                html_img = str(soup)</span><br><span class="line">                html = html_img.format(title=title, text=text, author=author, cretime=cretime)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                html = html_template.format(title=title, text=text, author=author, cretime=cretime)</span><br><span class="line"></span><br><span class="line">            <span class="keyword">if</span> topic.get(<span class="string">'question'</span>):</span><br><span class="line">                answer_author = topic.get(<span class="string">'answer'</span>).get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>, <span class="string">''</span>)</span><br><span class="line">                answer = topic.get(<span class="string">'answer'</span>).get(<span class="string">'text'</span>, <span class="string">""</span>)</span><br><span class="line">                answer = handle_link(answer)</span><br><span class="line"></span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                answer_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line"></span><br><span class="line">                answer = <span class="string">'【'</span> + answer_author + <span class="string">'】 回答：&lt;br&gt;'</span> + answer</span><br><span class="line">                soup_temp = BeautifulSoup(answer, <span class="string">'html.parser'</span>)</span><br><span class="line">                answer_tag.append(soup_temp)</span><br><span class="line"></span><br><span class="line">                soup.body.append(answer_tag)</span><br><span class="line">                html = str(soup) </span><br><span class="line">			</span><br><span class="line">            files = content.get(<span class="string">'files'</span>)</span><br><span class="line">            <span class="keyword">if</span> files:</span><br><span class="line">                files_content = <span class="string">'&lt;i&gt;文件列表(需访问网站下载) :&lt;br&gt;'</span></span><br><span class="line">                <span class="keyword">for</span> f <span class="keyword">in</span> files:</span><br><span class="line">                    files_content += f.get(<span class="string">'name'</span>) + <span class="string">'&lt;br&gt;'</span></span><br><span class="line">                files_content += <span class="string">'&lt;/i&gt;'</span></span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                files_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line">                soup_temp = BeautifulSoup(files_content, <span class="string">'html.parser'</span>)</span><br><span class="line">                files_tag.append(soup_temp)</span><br><span class="line">                soup.body.append(files_tag)</span><br><span class="line">                html = str(soup)</span><br><span class="line"></span><br><span class="line">            comments = topic.get(<span class="string">'show_comments'</span>)</span><br><span class="line">            <span class="keyword">if</span> DOWLOAD_COMMENTS and comments:</span><br><span class="line">                soup = BeautifulSoup(html, <span class="string">'html.parser'</span>)</span><br><span class="line">                hr_tag = soup.new_tag(<span class="string">'hr'</span>)</span><br><span class="line">                soup.body.append(hr_tag)</span><br><span class="line">                <span class="keyword">for</span> comment <span class="keyword">in</span> comments:</span><br><span class="line">                    comment_str = <span class="string">''</span></span><br><span class="line">                    <span class="keyword">if</span> comment.get(<span class="string">'repliee'</span>):</span><br><span class="line">                        comment_str = <span class="string">'['</span> + comment.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>) + <span class="string">' 回复 '</span> + comment.get(<span class="string">'repliee'</span>).get(<span class="string">'name'</span>) + <span class="string">'] : '</span> + handle_link(comment.get(<span class="string">'text'</span>))</span><br><span class="line">                    <span class="keyword">else</span>:</span><br><span class="line">                        comment_str = <span class="string">'['</span> + comment.get(<span class="string">'owner'</span>).get(<span class="string">'name'</span>) + <span class="string">'] : '</span> + handle_link(comment.get(<span class="string">'text'</span>))</span><br><span class="line"></span><br><span class="line">                    comment_tag = soup.new_tag(<span class="string">'p'</span>)</span><br><span class="line">                    soup_temp = BeautifulSoup(comment_str, <span class="string">'html.parser'</span>)</span><br><span class="line">                    comment_tag.append(soup_temp)</span><br><span class="line">                    soup.body.append(comment_tag)</span><br><span class="line">                html = str(soup)</span><br><span class="line"></span><br><span class="line">            htmls.append(html)</span><br><span class="line"></span><br><span class="line">    # DEBUG 仅导出部分数据时使用</span><br><span class="line">    <span class="keyword">if</span> DEBUG and num &gt;= DEBUG_NUM:</span><br><span class="line">       <span class="keyword">return</span> htmls</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> OVER_DATE_BREAK:</span><br><span class="line">        <span class="keyword">return</span> htmls</span><br><span class="line"></span><br><span class="line">    next_page = rsp.json().get(<span class="string">'resp_data'</span>).get(<span class="string">'topics'</span>)</span><br><span class="line">    <span class="keyword">if</span> next_page:</span><br><span class="line">        create_time = next_page[<span class="number">-1</span>].get(<span class="string">'create_time'</span>)</span><br><span class="line">        <span class="keyword">if</span> create_time[<span class="number">20</span>:<span class="number">23</span>] == <span class="string">"000"</span>:</span><br><span class="line">            end_time = create_time[:<span class="number">20</span>]+<span class="string">"999"</span>+create_time[<span class="number">23</span>:]</span><br><span class="line">            str_date_time = end_time[:<span class="number">19</span>]</span><br><span class="line">            delta = datetime.timedelta(seconds=<span class="number">1</span>)</span><br><span class="line">            date_time = datetime.datetime.strptime(str_date_time, <span class="string">'%Y-%m-%dT%H:%M:%S'</span>)</span><br><span class="line">            date_time = date_time - delta</span><br><span class="line">            str_date_time = date_time.strftime(<span class="string">'%Y-%m-%dT%H:%M:%S'</span>)</span><br><span class="line">            end_time = str_date_time + end_time[<span class="number">19</span>:]</span><br><span class="line">        <span class="keyword">else</span> :</span><br><span class="line">            res = int(create_time[<span class="number">20</span>:<span class="number">23</span>])<span class="number">-1</span></span><br><span class="line">            end_time = create_time[:20]+str(res).zfill(3)+create_time[23:] # zfill 函数补足结果前面的零，始终为3位数</span><br><span class="line">        end_time = quote(end_time)</span><br><span class="line">        <span class="keyword">if</span> len(end_time) == <span class="number">33</span>:</span><br><span class="line">            end_time = end_time[:<span class="number">24</span>] + <span class="string">'0'</span> + end_time[<span class="number">24</span>:]</span><br><span class="line">        next_url = start_url + <span class="string">'&amp;end_time='</span> + end_time</span><br><span class="line">        print(next_url)</span><br><span class="line">        get_data(next_url)</span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> htmls,t</span><br><span class="line"></span><br><span class="line">def handle_link(text):</span><br><span class="line">    soup = BeautifulSoup(text, <span class="string">"html.parser"</span>)</span><br><span class="line"></span><br><span class="line">    mention = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'mention'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(mention):</span><br><span class="line">        <span class="keyword">for</span> m <span class="keyword">in</span> mention:</span><br><span class="line">            mention_name = m.attrs[<span class="string">'title'</span>]</span><br><span class="line">            new_tag = soup.new_tag(<span class="string">'span'</span>)</span><br><span class="line">            new_tag.string = mention_name</span><br><span class="line">            m.replace_with(new_tag)</span><br><span class="line"></span><br><span class="line">    hashtag = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'hashtag'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(hashtag):</span><br><span class="line">        <span class="keyword">for</span> tag <span class="keyword">in</span> hashtag:</span><br><span class="line">            tag_name = unquote(tag.attrs[<span class="string">'title'</span>])</span><br><span class="line">            new_tag = soup.new_tag(<span class="string">'span'</span>)</span><br><span class="line">            new_tag.string = tag_name</span><br><span class="line">            tag.replace_with(new_tag)</span><br><span class="line"></span><br><span class="line">    links = soup.find_all(<span class="string">'e'</span>, attrs=&#123;<span class="string">'type'</span> : <span class="string">'web'</span>&#125;)</span><br><span class="line">    <span class="keyword">if</span> len(links):</span><br><span class="line">        <span class="keyword">for</span> link <span class="keyword">in</span> links:</span><br><span class="line">            title = unquote(link.attrs[<span class="string">'title'</span>])</span><br><span class="line">            href = unquote(link.attrs[<span class="string">'href'</span>])</span><br><span class="line">            new_a_tag = soup.new_tag(<span class="string">'a'</span>, href=href)</span><br><span class="line">            new_a_tag.string = title</span><br><span class="line">            link.replace_with(new_a_tag)</span><br><span class="line"></span><br><span class="line">    text = str(soup)</span><br><span class="line">    text = re.sub(r<span class="string">'&lt;e[^&gt;]*&gt;'</span>, <span class="string">''</span>, text).strip()</span><br><span class="line">    text = text.replace(<span class="string">'\n'</span>, <span class="string">'&lt;br&gt;'</span>)</span><br><span class="line">    <span class="keyword">return</span> text</span><br><span class="line"></span><br><span class="line">def make_pdf(htmls):</span><br><span class="line">    html_files = []</span><br><span class="line">    <span class="keyword">for</span> index, html <span class="keyword">in</span> enumerate(htmls):</span><br><span class="line">        file = str(index) + <span class="string">".html"</span></span><br><span class="line">        html_files.append(file)</span><br><span class="line">        <span class="keyword">with</span> open(file, <span class="string">"w"</span>, encoding=<span class="string">"utf-8"</span>) <span class="keyword">as</span> f:</span><br><span class="line">            f.write(html)</span><br><span class="line"></span><br><span class="line">    options = &#123;</span><br><span class="line">        <span class="string">"user-style-sheet"</span>: <span class="string">"temp.css"</span>,</span><br><span class="line">        <span class="string">"page-size"</span>: <span class="string">"Letter"</span>,</span><br><span class="line">        <span class="string">"margin-top"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-right"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-bottom"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"margin-left"</span>: <span class="string">"0.75in"</span>,</span><br><span class="line">        <span class="string">"encoding"</span>: <span class="string">"UTF-8"</span>,</span><br><span class="line">        <span class="string">"custom-header"</span>: [(<span class="string">"Accept-Encoding"</span>, <span class="string">"gzip"</span>)],</span><br><span class="line">        <span class="string">"cookie"</span>: [</span><br><span class="line">            (<span class="string">"cookie-name1"</span>, <span class="string">"cookie-value1"</span>), (<span class="string">"cookie-name2"</span>, <span class="string">"cookie-value2"</span>)</span><br><span class="line">        ],</span><br><span class="line">        <span class="string">"outline-depth"</span>: <span class="number">10</span>,</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">try</span>:</span><br><span class="line">        pdfkit.from_file(html_files, PDF_FILE_NAME, options=options)</span><br><span class="line">    except Exception <span class="keyword">as</span> e:</span><br><span class="line">        pass</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> DELETE_HTML_WHEN_DONE:</span><br><span class="line">        <span class="keyword">for</span> file <span class="keyword">in</span> html_files:</span><br><span class="line">            os.remove(file)</span><br><span class="line"></span><br><span class="line">    print(<span class="string">"电子书生成成功！"</span>)</span><br><span class="line">def wordimage(content):</span><br><span class="line">    comments = <span class="string">''</span></span><br><span class="line">    <span class="keyword">for</span> k <span class="keyword">in</span> range(len(content)):</span><br><span class="line">        comments = comments + (str(content[k])).strip()</span><br><span class="line">    doc = PyQuery(comments)</span><br><span class="line">    #使用正则表达式去除标点符号</span><br><span class="line">    #pattern = re.compile(r'[\u4e00-\u9fa5]+')</span><br><span class="line">    #filterdata = re.findall(pattern, comments)</span><br><span class="line">    #cleaned_comments = ''.join(filterdata)</span><br><span class="line">    cleaned_comments = <span class="string">''</span>.join(jieba.cut(doc.text()))</span><br><span class="line">    print(cleaned_comments)</span><br><span class="line">    #使用结巴分词进行中文分词</span><br><span class="line">    segment = jieba.lcut(cleaned_comments)</span><br><span class="line">    words_df=pd.DataFrame(&#123;<span class="string">'segment'</span>:segment&#125;)</span><br><span class="line">    #去掉停用词，先下载https://github.com/wendy1990/short_text_classification/blob/master/conf/stopwords.txt</span><br><span class="line">    stopwords=pd.read_csv("stopwords.txt",index_col=False,quoting=3,sep="\t",names=['stopword'], encoding='utf-8')#quoting=3全不引用words_df=words_df[~words_df.segment.isin(stopwords.stopword)]</span><br><span class="line">    #统计词频 看看哪些词出现概率高</span><br><span class="line">    words_stat=words_df.groupby(by=[<span class="string">'segment'</span>])[<span class="string">'segment'</span>].agg(&#123;<span class="string">"计数"</span>:np.size&#125;)</span><br><span class="line">    words_stat=words_stat.reset_index().sort_values(by=[<span class="string">"计数"</span>],ascending=False)</span><br><span class="line">    #print(words_stat.head(1000).values)</span><br><span class="line">    stopwords = set(STOPWORDS)</span><br><span class="line">    #用词云进行显示</span><br><span class="line">    wordcloud=WordCloud(font_path=<span class="string">"c:\windos\fonts\simhei.ttf"</span>,background_color=<span class="string">"white"</span>,max_font_size=<span class="number">80</span>,stopwords=STOPWORDS.add(<span class="string">"said"</span>)).generate(cleaned_comments)</span><br><span class="line">    #word_frequence = &#123;x[0]:x[1] for x in words_stat.head(1000).values&#125;</span><br><span class="line">    #word_frequence_list = []</span><br><span class="line">    #for key in word_frequence:</span><br><span class="line">    #    temp = (key,word_frequence[key])</span><br><span class="line">    #    word_frequence_list.append(temp)</span><br><span class="line">    #wordcloud=wordcloud.fit_words(word_frequence_list)</span><br><span class="line">    #plt.imshow(wordcloud)</span><br><span class="line">    #plt.show()</span><br><span class="line">    plt.imshow(wordcloud, interpolation=<span class="string">"bilinear"</span>)</span><br><span class="line">    plt.axis(<span class="string">"off"</span>)</span><br><span class="line">    plt.show()</span><br><span class="line">    wordcloud.to_file(<span class="string">'zsxq_tool.jpg'</span>)</span><br><span class="line"><span class="keyword">if</span> __name__ == <span class="string">'__main__'</span>:</span><br><span class="line">    images_path = r<span class="string">'./images'</span></span><br><span class="line">    <span class="keyword">if</span> DOWLOAD_PICS:</span><br><span class="line">        <span class="keyword">if</span> os.path.exists(images_path):</span><br><span class="line">            shutil.rmtree(images_path)</span><br><span class="line">        os.mkdir(images_path)</span><br><span class="line"></span><br><span class="line">    # 仅精华</span><br><span class="line">    #start_url = 'https://api.zsxq.com/v1.10/groups/481818518558/topics?scope=digests&amp;count=30'</span><br><span class="line">    # 全部</span><br><span class="line">    #start_url = 'https://api.zsxq.com/v1.10/groups/481818518558/topics?count=30'</span><br><span class="line">    start_url = <span class="string">''</span></span><br><span class="line">    <span class="keyword">if</span> ONLY_DIGESTS:</span><br><span class="line">        start_url = <span class="string">'https://api.zsxq.com/v1.10/groups/'</span> + GROUP_ID + <span class="string">'/topics?scope=digests&amp;count='</span> + str(COUNTS_PER_TIME)</span><br><span class="line">    <span class="keyword">else</span>:</span><br><span class="line">        start_url = <span class="string">'https://api.zsxq.com/v1.10/groups/'</span> + GROUP_ID + <span class="string">'/topics?scope=all&amp;count='</span> + str(COUNTS_PER_TIME)</span><br><span class="line"></span><br><span class="line">    url = start_url</span><br><span class="line">    <span class="keyword">if</span> FROM_DATE_TO_DATE and LATE_DATE.strip():</span><br><span class="line">        url = start_url + <span class="string">'&amp;end_time='</span> + quote(LATE_DATE.strip())</span><br><span class="line">    data = get_data(url)</span><br><span class="line">    #print(data[1])</span><br><span class="line">    #wordimage(data[1])</span><br><span class="line">    #make_pdf(data[0])</span><br><span class="line"></span><br><span class="line">    <span class="keyword">if</span> DOWLOAD_PICS and DELETE_PICS_WHEN_DONE:</span><br><span class="line">        shutil.rmtree(images_path)</span><br></pre></td></tr></table></figure>
<p>执行效果见图<br><img src="https://upload-images.jianshu.io/upload_images/17817191-07b0a8ce1442ea8a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>生成的PDF文件有点大，565页 ，50多M，主要是评论和图片都下载了，不下载的话5M差不多。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-87020a336d8967cf.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>为了验证付费星球也能下载，我还建了个付费星球<a href="https://wx.zsxq.com/dweb2/index/group/224445125221，以后也会经常更新。" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/224445125221，以后也会经常更新。</a><br> <img src="https://upload-images.jianshu.io/upload_images/17817191-5f36737e35dbbff9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-aeca99f305f23d61.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>评论和回复也下载了<br><img src="https://upload-images.jianshu.io/upload_images/17817191-dc036a80532a5ce1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-70a44013887caa7d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>2019.6.19创建的星球<br><img src="https://upload-images.jianshu.io/upload_images/17817191-1818e57b45d5027b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>Python生成的词云效果不大好，还没过滤好无用的词。</p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-1a497d8234026372.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>文字版也导出到TXT了。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-a86943499b797fd1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>在词云网站 <a href="http://cloud.niucodata.com/" target="_blank" rel="noopener">http://cloud.niucodata.com/</a>  将下载的文字放进去就能看到词频统计和词云图。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-aace6c380516b87f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-ec6b0234c3353915.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>如果你想看下载这个PDF，公众号回复 <code>星球</code> 获取PDF和文字版。<br><a href="https://pan.baidu.com/s/1pJ-nLiLLMlK08_vwoE0ZuA" target="_blank" rel="noopener">https://pan.baidu.com/s/1pJ-nLiLLMlK08_vwoE0ZuA</a><br><a href="https://pan.baidu.com/s/1TcYpM4x5td2GLjkltHJhBQ" target="_blank" rel="noopener">https://pan.baidu.com/s/1TcYpM4x5td2GLjkltHJhBQ</a></p>
<h3 id="wkhtmltopdf"><a href="#wkhtmltopdf" class="headerlink" title="wkhtmltopdf"></a>wkhtmltopdf</h3><p>使用过程中发现wkhtmltopdf这个工具很好用 <a href="https://wkhtmltopdf.org/" target="_blank" rel="noopener">https://wkhtmltopdf.org/</a> 这个软件很方便在命令行生成图片和PDF，<br>执行 <code>wkhtmltoimage www.qq.com qq.png</code>，就会在当前目录下生成了一张png图片。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">$ wkhtmltoimage www.qq.com qq.png</span><br><span class="line">Loading page (<span class="number">1</span>/<span class="number">2</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)                     ] <span class="number">44</span>%</span><br><span class="line">libpng warning: iCCP: known incorrect sRGB profile           ] <span class="number">64</span>%</span><br><span class="line">Rendering (<span class="number">2</span>/<span class="number">2</span>)</span><br><span class="line">Done</span><br><span class="line"></span><br><span class="line"> $ wkhtmltopdf www.qq.com qq.pdf</span><br><span class="line">Loading pages (<span class="number">1</span>/<span class="number">6</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)                     ] <span class="number">44</span>%</span><br><span class="line">libpng warning: iCCP: known incorrect sRGB profile===&gt;       ] <span class="number">87</span>%</span><br><span class="line">Counting pages (<span class="number">2</span>/<span class="number">6</span>)</span><br><span class="line">QFont::setPixelSize: Pixel size &lt;= <span class="number">0</span> (<span class="number">0</span>)=====================] <span class="built_in">Object</span> <span class="number">1</span> <span class="keyword">of</span> <span class="number">1</span></span><br><span class="line">Resolving links (<span class="number">4</span>/<span class="number">6</span>)</span><br><span class="line">Loading headers and footers (<span class="number">5</span>/<span class="number">6</span>)</span><br><span class="line">Printing pages (<span class="number">6</span>/<span class="number">6</span>)</span><br><span class="line">Done</span><br><span class="line">Warning: Received createRequest signal on a disposed ResourceObject<span class="string">'s NetworkAccessManager. This might be an indication of an iframe taking too long to load.</span></span><br><span class="line"><span class="string">Warning: Received createRequest signal on a disposed ResourceObject'</span>s NetworkAccessManager. This might be an indication <span class="keyword">of</span> an iframe taking too long to load.</span><br></pre></td></tr></table></figure></p>
<p><img src="https://upload-images.jianshu.io/upload_images/17817191-b20148873f1dc5a0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>这个命令还可以增加一些参数，比如常用的设置宽高，图片质量等参数<br>执行<code>wkhtmltoimage --crop-w 410 --width 410 --quality 50 www.bing.com bing2.png</code>看看<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line">--crop-w <span class="number">410</span>：截图宽度<span class="number">410</span>px</span><br><span class="line"></span><br><span class="line">--width <span class="number">410</span>：浏览器模拟宽度<span class="number">410</span>px</span><br><span class="line"></span><br><span class="line">--quality <span class="number">50</span>：图片质量(这个值越大，图片质量越高，当然文件也会比较大)</span><br><span class="line"></span><br><span class="line">还有更多参数用法，请 wkhtmltoimage -h查看。</span><br><span class="line">HTML转pdf同理，wkhtmltopdf -h</span><br><span class="line">wkhtmltopdf -v</span><br><span class="line">--collate             当输出多个副本时进行校验（这是默认设置）</span><br><span class="line">    --no-collate          当输出多个副本时不进行校验</span><br><span class="line">    --cookie-jar &lt;path&gt;   从提供的 JAR 文件中读写 cookie 数据</span><br><span class="line">    --copies &lt;number&gt;     设置输出副本的数量（默认主 <span class="number">1</span>)，其实为 <span class="number">1</span> 就够了</span><br><span class="line">-d, --dpi &lt;dpi&gt;           指定一个要分辨率（这在 X11 系统中并没有什么卵用）</span><br><span class="line">-H, --extended-help       相对 -h 参数，显示更详细的说明文档</span><br><span class="line">-g, --grayscale           指定以灰度图生成 PDF 文档。占用的空间更小</span><br><span class="line">-h, --help                显示帮助信息</span><br><span class="line">    --htmldoc             输出程序的 html 帮助文档</span><br><span class="line">    --image-dpi &lt;integer&gt; 当页面中有内嵌的图片时，</span><br><span class="line">                          会下载此命令行参数指定尺寸的图片（默认值是 <span class="number">600</span>)</span><br><span class="line">    --image-quality &lt;interger&gt; 当使用 jpeg 算法压缩图片时使用这个参数指定的质量（默认为 <span class="number">94</span>)</span><br><span class="line">    --license             输出授权信息并退出</span><br><span class="line">-l, --lowquality          生成低质量的 PDF/PS , 能够很好的节约最终生成文档所占存储空间</span><br><span class="line">    --manpage             输出程序的手册页</span><br><span class="line">-B, --margin-bottom &lt;unitreal&gt; 设置页面的 底边距</span><br><span class="line">-L, --margin-left &lt;unitreal&gt;   设置页面的 左边距 （默认是 <span class="number">10</span>mm)</span><br><span class="line">-R, --margin-right &lt;unitreal&gt;  设置页面的 右边距 （默认是 <span class="number">10</span>mm)</span><br><span class="line">-T, --margin-top &lt;unitreal&gt;    设置页面的 上边距</span><br><span class="line">-O, --orientation &lt;orientation&gt; 设置为“风景 (Landscape)”或“肖像 (Portrait)”模式，</span><br><span class="line">                                默认是肖像模块 (Portrait)</span><br><span class="line">    --page-height &lt;unitreal&gt;   页面高度</span><br><span class="line">-s, --page-size &lt;Size&gt;         设置页面的尺寸，如：A4,Letter 等，默认是：A4</span><br><span class="line">    --page-width &lt;unitreal&gt;    页面宽度</span><br><span class="line">    --no-pdf-compression       不对 PDF 对象使用丢失少量信息的压缩算法，不建议使用些参数，</span><br><span class="line">                               因为生成的 PDF 文件会非常大。</span><br><span class="line">-q, --quiet                    静态模式，不在标准输出中打印任何信息</span><br><span class="line">    --read-args-<span class="keyword">from</span>-stdin     从标准输入中读取命令行参数，后续会有针对此指令的详细介绍，</span><br><span class="line">                               请参见 **从标准输入获取参数**</span><br><span class="line">    --readme                   输出程序的 readme 文档</span><br><span class="line">    --title &lt;text&gt;             生成的 PDF 文档的标题，如果不指定则使用第一个文档的标题</span><br><span class="line">-V, --version                  输出版本信息后退出</span><br></pre></td></tr></table></figure></p>
<p>wkhtmltopdf “/home/wang/test.html” /home/wang/test.pdf</p>
<p>wkhtmltopdf “<a href="https://www.jd.com&quot;" target="_blank" rel="noopener">https://www.jd.com&quot;</a> /home/wang/jj.pdf</p>
<h3 id="中文乱码"><a href="#中文乱码" class="headerlink" title="中文乱码"></a>中文乱码</h3><p>如果你是在 linux 上执行脚本可能会出现中文乱码，解决方法就是从windows拷贝宋体字体文件 <code>c:\windos\fonts\simhei.ttf</code>到<code>/usr/share/fonts/</code>下<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">cd /usr/share/fonts/</span><br><span class="line">cp simhei.ttf .</span><br><span class="line">mkfontscale</span><br><span class="line">mkfontdir</span><br><span class="line">fc-cache</span><br></pre></td></tr></table></figure></p>
<p>再次执行fc-list可以看到已经安装的字体了。</p>
<h3 id="phantomjs"><a href="#phantomjs" class="headerlink" title="phantomjs"></a>phantomjs</h3><p>除了wkhtmltopdf生成PDF还有phantomjs 的功能也很强大，做爬虫应用，抓取网页数据、网页截屏、页面访问自动化等。<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">set_time_limit(<span class="number">0</span>);</span><br><span class="line">$path = <span class="string">'/bin/phantomjs'</span>;		<span class="comment">//phantomjs路径</span></span><br><span class="line"><span class="comment">//$jsPath = '/home/test/phantomjs/examples/test.js';	</span></span><br><span class="line">$url = <span class="string">'https://baidu.com/'</span>;		<span class="comment">//要抓取的网页</span></span><br><span class="line">$out = <span class="string">'baidu.png'</span>;			<span class="comment">//图片保存路径</span></span><br><span class="line">$cmd = <span class="string">"$path --output-encoding=utf-8 $jsPath $url $out"</span>;</span><br><span class="line">echo $cmd;</span><br><span class="line">system($cmd);</span><br><span class="line"></span><br><span class="line">创建test.js</span><br><span class="line"><span class="string">``</span><span class="string">`js</span></span><br><span class="line"><span class="string">var page = require('webpage').create();</span></span><br><span class="line"><span class="string">var args = require('system').args;</span></span><br><span class="line"><span class="string">var url = args[1];</span></span><br><span class="line"><span class="string">var filename = args[2];</span></span><br><span class="line"><span class="string">page.open(url, function () &#123;</span></span><br><span class="line"><span class="string">    page.render(filename);</span></span><br><span class="line"><span class="string">    phantom.exit();</span></span><br><span class="line"><span class="string">&#125;);</span></span><br></pre></td></tr></table></figure></p>
<p>执行 /home/test/phantomjs/bin/phantomjs  test.js   <a href="http://www.baidu.com" target="_blank" rel="noopener">http://www.baidu.com</a>   baidu.png</p>
<h3 id="puppeteer"><a href="#puppeteer" class="headerlink" title="puppeteer"></a>puppeteer</h3><p>puppeteer 是 chrome 提供的一个无头浏览器，它是替代 phantomjs 的一个替代品，多用于实现自动化测试。官方仓库地址：<a href="https://github.com/GoogleChrome/puppeteer,Puppeteer" target="_blank" rel="noopener">https://github.com/GoogleChrome/puppeteer,Puppeteer</a> 能用来干啥，有哪些功能：<br>生成页面的截图和 PDF<br>爬取网站内容<br>模拟登陆、自动提交表单，UI 测试，键盘输入等<br>使用最新的 JavaScript 和浏览器功能，直接在最新版本的 Chrome 中运行测试。<br>捕获网站的时间线跟踪，以帮助诊断性能问题<br>测试 Chrome 扩展<br>screenshoteer是基于Puppeteer 的命令行工具<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">npm i -g screenshoteer</span><br><span class="line">screenshoteer --url https:<span class="comment">//nicelinks.site/</span></span><br><span class="line">λ screenshoteer --url http:<span class="comment">//qq.com</span></span><br><span class="line">http:<span class="comment">//qq.com</span></span><br><span class="line"><span class="literal">true</span></span><br><span class="line">腾讯首页</span><br></pre></td></tr></table></figure></p>
<h3 id="pdfkit"><a href="#pdfkit" class="headerlink" title="pdfkit"></a>pdfkit</h3><p>这是个Python生成PDF的库<br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">&gt;&gt;&gt; <span class="keyword">import</span> pdfkit</span><br><span class="line">&gt;&gt;&gt; config = pdfkit.configuration(wkhtmltopdf=r<span class="string">'d:\wkhtmltopdf\bin\wkhtmltopdf.exe'</span>)</span><br><span class="line">&gt;&gt;&gt; pdfkit.from_url(<span class="string">'https://sogou.com'</span>, <span class="string">'sogou.pdf'</span>, configuration=config)</span><br><span class="line">Loading pages (<span class="number">1</span>/<span class="number">6</span>)</span><br><span class="line">Counting pages (<span class="number">2</span>/<span class="number">6</span>)</span><br><span class="line">Resolving links (<span class="number">4</span>/<span class="number">6</span>)</span><br><span class="line">Loading headers and footers (<span class="number">5</span>/<span class="number">6</span>)</span><br><span class="line">Printing pages (<span class="number">6</span>/<span class="number">6</span>)</span><br><span class="line">Done</span><br><span class="line">True</span><br></pre></td></tr></table></figure></p>
<p>也可以将本地文件生成PDF<br> pdfkit.from_file(‘test.html’, r’D:\test’ + id + ‘.pdf’, configuration=config)</p>
<h3 id="popple"><a href="#popple" class="headerlink" title="popple"></a>popple</h3><p>pdf软件 poppler  <a href="https://github.com/freedesktop/poppler" target="_blank" rel="noopener">https://github.com/freedesktop/poppler</a><br><figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">$ pdfinfo   知识星球_苏生不惑.pdf</span><br><span class="line">Title:</span><br><span class="line">Creator:        wkhtmltopdf <span class="number">0.12</span><span class="number">.5</span></span><br><span class="line">Producer:       Qt <span class="number">4.8</span><span class="number">.7</span></span><br><span class="line">CreationDate:   <span class="number">09</span>/<span class="number">04</span>/<span class="number">19</span> <span class="number">21</span>:<span class="number">02</span>:<span class="number">59</span> </span><br><span class="line">Tagged:         no</span><br><span class="line">UserProperties: no</span><br><span class="line">Suspects:       no</span><br><span class="line">Form:           none</span><br><span class="line">JavaScript:     no</span><br><span class="line">Pages:          <span class="number">565</span></span><br><span class="line">Encrypted:      no</span><br><span class="line">Page size:      <span class="number">612</span> x <span class="number">792</span> pts (letter)</span><br><span class="line">Page rot:       <span class="number">0</span></span><br><span class="line">File size:      <span class="number">0</span> bytes</span><br><span class="line">Optimized:      no</span><br><span class="line">PDF version:    <span class="number">1.4</span></span><br><span class="line"></span><br><span class="line"> </span><br><span class="line">λ pdf</span><br><span class="line">pdfdetach.exe    pdfimages.exe    pdfseparate.exe  pdftohtml.exe    pdftops.exe      pdfunite.exe</span><br><span class="line">pdffonts.exe     pdfinfo.exe      pdftocairo.exe   pdftoppm.exe     pdftotext.exe</span><br></pre></td></tr></table></figure></p>
<h3 id="资源"><a href="#资源" class="headerlink" title="资源"></a>资源</h3><p>pdf合并工具pdftk <a href="https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/" target="_blank" rel="noopener">https://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/</a><br>  <a href="https://segmentfault.com/a/1190000018765575" target="_blank" rel="noopener">初识wkhtmltopdf</a><br>中文字体<a href="https://github.com/sonatype/maven-guide-zh/raw/master/content-zh/src/main/resources/fonts/simsun.ttc" target="_blank" rel="noopener">https://github.com/sonatype/maven-guide-zh/raw/master/content-zh/src/main/resources/fonts/simsun.ttc</a><br><a href="http://einverne.github.io/post/2019/01/html-to-pdf.html" target="_blank" rel="noopener">http://einverne.github.io/post/2019/01/html-to-pdf.html</a><br><a href="https://hacpai.com/article/1544237764458" target="_blank" rel="noopener"> Web 屏幕截图工具</a><br>使用 Laravel-snappy 生成 PDF 踩坑记录<a href="https://learnku.com/articles/25922" target="_blank" rel="noopener">https://learnku.com/articles/25922</a><br> <a href="https://github.com/Kozea/WeasyPrint" target="_blank" rel="noopener">https://github.com/Kozea/WeasyPrint</a><br>基于前端技术生成PDF方案<a href="https://juejin.im/post/5d036a78f265da1bce3dcd40" target="_blank" rel="noopener">https://juejin.im/post/5d036a78f265da1bce3dcd40</a><br> <a href="https://segmentfault.com/a/1190000018701596" target="_blank" rel="noopener">HTML转成PDF的4个方案及实现</a><br>js导出图片与pdf的方案<a href="https://www.jianshu.com/p/408748cfa76e" target="_blank" rel="noopener">https://www.jianshu.com/p/408748cfa76e</a><br> <a href="https://brianshen1990.github.io/HTML_To_PDF_Solutions.html" title="Permalink to HTML To PDF Solutions / HTML 转换 PDF 解决方案" target="_blank" rel="noopener">HTML 转换 PDF 解决方案</a><br>抓取网页生成 PDF <a href="http://jartto.wang/2018/10/13/nodejs-pdf/" target="_blank" rel="noopener">http://jartto.wang/2018/10/13/nodejs-pdf/</a></p>
<h3 id="星球导航网站"><a href="#星球导航网站" class="headerlink" title="星球导航网站"></a>星球导航网站</h3><p><a href="https://tooltowin.com/zsxq/1124/" target="_blank" rel="noopener">https://tooltowin.com/zsxq/1124/</a><br><a href="https://www.101ker.com/p/2284" target="_blank" rel="noopener">https://www.101ker.com/p/2284</a><br> <a href="https://xiaogenhua.com/2019/05/20/caozsay/" target="_blank" rel="noopener">https://xiaogenhua.com/2019/05/20/caozsay/</a><br><a href="https://www.kindkp.com/" target="_blank" rel="noopener">https://www.kindkp.com/</a> </p>
<h3 id="推荐星球"><a href="#推荐星球" class="headerlink" title="推荐星球"></a>推荐星球</h3><p>这段时间也加入了不少星球，推荐一个我几乎每天看的星球，为什么每天看呢，因为星主每天分享，太勤快了，星球名叫 <code>风巢套利日享（限免）</code>，免费的<a href="https://wx.zsxq.com/dweb2/index/group/554228114224" target="_blank" rel="noopener">https://wx.zsxq.com/dweb2/index/group/554228114224</a><br>这是这个星球的词云，看到这些关键词，你心动了吗？点击阅读原文或扫码加入星球。<br><img src="https://upload-images.jianshu.io/upload_images/17817191-2211005aef836d43.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br><img src="https://upload-images.jianshu.io/upload_images/17817191-5d2c9bc76a82c136.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>路人甲<br><img src="https://upload-images.jianshu.io/upload_images/17817191-410eb89d1155c975.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>不安分的<br><img src="https://upload-images.jianshu.io/upload_images/17817191-2b1906c35e8a37da.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>大门口<br><img src="https://upload-images.jianshu.io/upload_images/17817191-5bd614a2d0559d20.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p>最后如果你也想导出哪个星球，拉我进去帮你导出来。</p>
<p>推荐阅读:</p>
<p><a href="https://mp.weixin.qq.com/s/MUIPj4OgSxAeRl5Hk-2tuw" target="_blank" rel="noopener">没有提取码怎么获取百度网盘资源？</a></p>
<p><a href="https://mp.weixin.qq.com/s/Xz1m-mqtCcBF_4hmGCpkUQ" target="_blank" rel="noopener">如何发一条空白的朋友圈</a></p>
<p><a href="https://mp.weixin.qq.com/s/_3AeNahwbs8c3UJ0is1t4A" target="_blank" rel="noopener">如何在电脑上登陆多个微信</a></p>
<p><a href="https://mp.weixin.qq.com/s/qyFvOgHf1mXwPKO0tQwUyg" target="_blank" rel="noopener">如何提取公积金 9 天到账</a></p>
<p><a href="https://mp.weixin.qq.com/s/1omFkK5PPyeJEzUTagj9qg" target="_blank" rel="noopener">免费在线听周杰伦歌曲</a></p>
<p><a href="https://mp.weixin.qq.com/s/eGDO0Y8el_dsEyriCoAgog" target="_blank" rel="noopener">那些你可能不知道的微信奇技淫巧</a></p>
<p><a href="https://mp.weixin.qq.com/s/k5lBwiDzGgSU3fh2v2Rw9A" target="_blank" rel="noopener">如何在豆瓣租房小组快速找到满意的房子</a></p>
<p><a href="https://mp.weixin.qq.com/s/sng3uK9Nge1OD2gc5DDuZg" target="_blank" rel="noopener">那些你可能用得上的简历写作工具</a></p>
<p><a href="https://mp.weixin.qq.com/s/adJFh_9LH0N-vvvYaiQqXg" target="_blank" rel="noopener">Chrome 浏览器扩展神器油猴</a></p>
<p><a href="https://mp.weixin.qq.com/s/mI5kubVY2t5jwJ9ub7A1iA" target="_blank" rel="noopener">我的新浪工作日常</a></p>
<h3 id="公众号：苏生不惑"><a href="#公众号：苏生不惑" class="headerlink" title="公众号：苏生不惑"></a>公众号：苏生不惑</h3><p> <img src="https://upload-images.jianshu.io/upload_images/17817191-6e0079f95d4c0338.jpg?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="扫描二维码关注"></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/公众号/" rel="tag"># 公众号</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/09/16/那些你可能不知道的-bilibili-奇技淫巧/" rel="next" title="那些你可能不知道的 bilibili 奇技淫巧">
                <i class="fa fa-chevron-left"></i> 那些你可能不知道的 bilibili 奇技淫巧
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/09/30/上班工作压力太大-来玩个游戏解压下/" rel="prev" title="上班工作压力太大?来玩个游戏解压下">
                上班工作压力太大?来玩个游戏解压下 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
          <div class="comments" id="comments">
             <div id="gitment-container"></div>
         </div>
  




        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope="" itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">苏生不惑</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">382</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">37</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/sushengbuhuo" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mysusheng@gmail.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://plus.google.com/mysusheng" target="_blank" title="Google">
                      
                        <i class="fa fa-fw fa-google"></i>Google</a>
                  </span>
                
            </div>
          

          
          

          
          
            <div class="links-of-blogroll motion-element links-of-blogroll-block">
              <div class="links-of-blogroll-title">
                <i class="fa  fa-fw fa-link"></i>
                Links
              </div>
              <ul class="links-of-blogroll-list">
                
                  <li class="links-of-blogroll-item">
                    <a href="http://v2ex.com/" title="v2ex" target="_blank">v2ex</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.fanhaobai.com/" title="fanhaobai" target="_blank">fanhaobai</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="yuanxuxu" target="_blank">yuanxuxu</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.snail-c.cn/article" title="snail-c" target="_blank">snail-c</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://showcj.com/archives" title="showcj" target="_blank">showcj</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://vultr.aicnm.com/%E6%9C%80%E6%96%B0Vultr%E6%B3%A8%E5%86%8C%E5%8F%8AVPS%E8%B4%AD%E4%B9%B0%E5%9B%BE%E6%96%87%E6%95%99%E7%A8%8B/" title="vultr" target="_blank">vultr</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lucissfer.com/" title="lucissfer" target="_blank">lucissfer</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.csdn.net/fdipzone/article/details/79352685" title="傲雪星枫" target="_blank">傲雪星枫</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yoby123.cn/index.php/category/default/" title="小白的分享" target="_blank">小白的分享</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/p/5819995.html" title="PHP攻城狮" target="_blank">PHP攻城狮</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://blog.jiaojie.site/" title="php" target="_blank">php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://sphard.com/archives/" title="sphard" target="_blank">sphard</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://yuanxuxu.com/archives/" title="LNMP技术栈笔记" target="_blank">LNMP技术栈笔记</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.coding10.com/" title="学习 Laravel" target="_blank">学习 Laravel</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://shuwoom.com/?page_id=929" title="区块链学习指南" target="_blank">区块链学习指南</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://greenlightt.github.io/archives/" title="greenlightt" target="_blank">greenlightt</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.0php.net/archives/" title="0php" target="_blank">0php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.fordba.com/category/mysql" title="mysql" target="_blank">mysql</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.itcodemonkey.com/" title="程序员" target="_blank">程序员</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.yanshuo.me/r/v2ex" title="言说" target="_blank">言说</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.timiguo.com/archive.html" title="提米果的博客" target="_blank">提米果的博客</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://phpartisan.cn/news/112.html" title="phpartisan" target="_blank">phpartisan</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.cnblogs.com/52fhy/" title="飞鸿影" target="_blank">飞鸿影</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="http://www.54php.cn/" title="54php" target="_blank">54php</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.lazyman.vip/" title="营销" target="_blank">营销</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.njphper.com/archives/" title="做人呢最重要的就是开心" target="_blank">做人呢最重要的就是开心</a>
                  </li>
                
                  <li class="links-of-blogroll-item">
                    <a href="https://www.h57.pw/" title="php 初心者" target="_blank">php 初心者</a>
                  </li>
                
              </ul>
            </div>
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#为什么用星球"><span class="nav-number">1.</span> <span class="nav-text">为什么用星球</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#导出星球"><span class="nav-number">2.</span> <span class="nav-text">导出星球</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#wkhtmltopdf"><span class="nav-number">3.</span> <span class="nav-text">wkhtmltopdf</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#中文乱码"><span class="nav-number">4.</span> <span class="nav-text">中文乱码</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#phantomjs"><span class="nav-number">5.</span> <span class="nav-text">phantomjs</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#puppeteer"><span class="nav-number">6.</span> <span class="nav-text">puppeteer</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#pdfkit"><span class="nav-number">7.</span> <span class="nav-text">pdfkit</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#popple"><span class="nav-number">8.</span> <span class="nav-text">popple</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#资源"><span class="nav-number">9.</span> <span class="nav-text">资源</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#星球导航网站"><span class="nav-number">10.</span> <span class="nav-text">星球导航网站</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#推荐星球"><span class="nav-number">11.</span> <span class="nav-text">推荐星球</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#公众号：苏生不惑"><span class="nav-number">12.</span> <span class="nav-text">公众号：苏生不惑</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

<div class="copyright">&copy; <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">苏生不惑</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>



<div>
<span id="showDays"></span>

</div>

<span id="busuanzi_container_site_pv">
   总访问量:<span id="busuanzi_value_site_pv"></span>次
</span>



  <span class="post-meta-divider">|</span>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共773.8k字</span>
</div>
<script>
var birthDay = new Date("11/20/2018");
var now = new Date();
var duration = now.getTime() - birthDay.getTime();
var total= Math.floor(duration / (1000 * 60 * 60 * 24));
document.getElementById("showDays").innerHTML = "本站已运行 "+total+" 天";
</script>

        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      人次
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      次
    </span>
  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/canvas-nest/canvas-nest.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  











<script type="text/javascript">
    (function() {
        // 匿名函数，防止污染全局变量
        var utterances = document.createElement('script');
        utterances.type = 'text/javascript';
        utterances.async = true;
        utterances.setAttribute('issue-term','0')
        utterances.setAttribute('theme','')
        utterances.setAttribute('repo','sushengbuhuo/laravel_ioc_demo')
        utterances.crossorigin = 'anonymous';
        utterances.src = 'https://utteranc.es/client.js';
        // content 是要插入评论的地方
        document.getElementById('gitment-container').appendChild(utterances);
    })();
</script>


  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  

  

  
<script>
  ((window.gitter = {}).chat = {}).options = {
    //room替换成自己的聊天室名称即可，room的名称规则是：username/roomname
    room: 'sushengbuhuo-chat/mychat'
  };
</script>
<script src="https://sidecar.gitter.im/dist/sidecar.v1.js" async defer></script>

</body>
</html>
